<?php

namespace app\admin\common;

use app\admin\common\taglib\lv;
use service\StringService;
use service\UtilService;
use think\Config;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;


class Adminbase extends Controller
{
    protected  static $controller_name; //控制器名称
    protected  static $mode_name; //模快名称
    protected  static $action_name;//方法名称
    protected $param;

    protected static $admin_info;
    protected static $admin_ID;

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub

        // 请求参数
        $this->param = $this->request->param();
        // 分页基础默认值
        define('PERPAGE', isset($this->param['limit']) ? $this->param['limit'] : 20);
        define('PAGE', isset($this->param['page']) ? $this->param['page'] : 1);
        $this->Load_fun(); // 加载

        //登录检测
        if(!Session::has('admin_info.username')) {
            $this->redirect('/admin/login/index');
        }
        
        $admin_info = Session::get('admin_info');
        self::$admin_info = Db::name("admin_member")->where(['id'=>$admin_info['id']])->find();
        //判断管理员用户状态是否被拉黑
        if(self::$admin_info['status'] == 2){
            // 注销，重新登陆
            session('admin_info', null); //注销
            $this->redirect('/admin/login/index');
        }
        $this->assign("admin_info", self::$admin_info);

        $this->assign('no_referrer', false);
    }

    /**
     * @name: Load_fun
     * @describe:赋值模快名称和控制
     */
    protected function Load_fun(){
        self::$action_name=strtolower($this->request->action());
        self::$controller_name=strtolower($this->request->controller());
        self::$mode_name=strtolower($this->request->module());
        $this->assign(
            ['action'=>self::$action_name,'controller'=>self::$controller_name,'module'=>self::$mode_name]
        );

        // 上传配置
        $uploadConfig = Config::get('upload');
        //dump($uploadConfig);exit;

        // 图片配置
        $this->assign('uploadImgExt', $uploadConfig['image_config']['upload_image_ext']);
        $this->assign('uploadImgSize', $uploadConfig['image_config']['upload_image_size']);
        $this->assign('uploadImgMax', $uploadConfig['image_config']['upload_image_max']);

        // 视频配置
        $this->assign('uploadVideoExt', $uploadConfig['video_config']['upload_video_ext']);
        $this->assign('uploadVideoSize', $uploadConfig['video_config']['upload_video_size']);
        $this->assign('uploadVideoMax', $uploadConfig['video_config']['upload_video_max']);


        $this->assign("Lucky_Name","绿城无卡通行系统-管理后台"); //系统名称
        $this->assign("Version","1.0.1"); //系统版本
        $this->assign("Site_url",''); //系统名称

    }

    /**
     * @param $describe
     * @return bool|int
     * @name: AddLogs
     * @describe:添加操作日志
     */
    protected function AddLogs($describe){
        if(empty($describe)){
            $describe="暂无描述";
        }
        $ars = ['describe' => $describe, 'type' => 2, 'admin_id' => self::$admin_info['id'], 'admin_name' => self::$admin_info['username'], 'browse' => UtilService::getBrowser(), 'model' => self::$mode_name, 'controller' => self::$controller_name, 'action' => self::$action_name];
        $ars['msg'] = json_encode($this->request->request(),JSON_UNESCAPED_UNICODE);
        return addLogs($ars);
    }

    /**
     * @param $table
     * @param $id
     * @name: DeleteData
     * @describe: 删除
     */
    public function DeleteData($table,$id){
        if(empty($table)){
            $this->error("请指定表");
        }
        try{
            $res=db($table)->where(['id' => ['in', $id]])->delete();
            if($res){
                $this->success("删除成功");
            }else{
                $this->error("删除失败");
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }

    }

    public function GetButtonMenuLists($role_id){
        if(empty($role_id)){
            return [];
        }
        $role_id = explode(",",$role_id);
        //halt($role_id);
        $menu = model('AdminMenu');
        $da = [];
        $da_all  = $menu->GetMenuList(['is_menu'=>2]); //获取数据
        //halt($da_all);
        if(in_array(1,$role_id)){
            $da = $da_all;
        }
        else{
            $admin_role = model('AdminRole');
            //dump($role_id);exit;
            $infos = $admin_role->all(['id'=>['in',$role_id]])->toArray();
            //halt($infos);exit;
            $ids = [];
            foreach ($infos as $info){
                $rules = $info['rule'];
                if (isset($rules) && !empty($rules)) {
                    $ids = array_unique(array_merge($ids,StringService::string2array($rules)));
                }
            }
            //dump($ids);exit;
            if (!empty($da_all)) {
                foreach ($da_all as $v) {
                    if (in_array($v['id'], $ids)) {
                        $da[] = $v;
                    }
                }
            }
        }
        //dump($da);exit;
        $resdata = [];
        foreach ($da as $row){
            $resdata[$row['href']] = $row;
        }
        return $resdata;
    }



    /**
     * @return mixed
     * @name: _empty
     * @describe:空操作找不到页面
     */
    public function _empty()
    {
        return $this->fetch('public/404');
    }


}
